CC = gcc
BINARY = opc
LEX = flex
YACC = bison
DIFF = diff


# Flags
CFLAGS = -g -Wall
YACCFLAGS = -d -y -v 

OBJECTS = \
	main.o \
	symtab.o usrdef.o rulefuncs.o shared.o error.o semantic.o \
	ir.o ir_print.o ir_map_pascal.o \
	opt_constant_folding.o \
	opt_redundant_elimination.o \
	opt_value_numbering.o \
	opt_global_redundancy_elimination.o \
	lir.o lir_print.o lir_map_ir.o \
	gen.o \
	gen_backend_asm.o mem.o

all:    yacc lex $(OBJECTS) tests_prepare
	$(CC) -o $(BINARY) $(CFLAGS) y.tab.c lex.yy.c $(OBJECTS)

lex:
	$(LEX) pascal.l

yacc:
	$(YACC) $(YACCFLAGS) pascal.y
	@# Play tricks with yacc to prepend our shared definitions
	echo "#include \"shared.h\"" >> y.tab.h.tmp
	cat y.tab.h >> y.tab.h.tmp
	rm y.tab.h
	mv y.tab.h.tmp y.tab.h

.c.o:
	$(CC) -c $(CFLAGS) $<
clean:
	-rm -f core $(BINARY) out.c *~
	-rm -f lex.yy.c y.tab.c y.tab.h y.tab.h.tmp y.output
	-rm -f $(OBJECTS)
	-rm -f tests_lib_shared_auto.inc
	-rm -f tests_semantic/results/$(BINARY).{log,sum}
	-rm -f tests_semantic/*.output
	-rm -f tests_semantic/out.c
	-rm -f tests_semantic/a.out
	-rm -f tests_opt/results/$(BINARY).{log,sum}
	-rm -f tests_opt/*.output
	-rm -f tests_opt/out.c
	-rm -f tests_opt/a.out
	-rm -f tests_gen/results/$(BINARY).{log,sum}
	-rm -f tests_gen/*.output
	-rm -f tests_gen/out.c
	-rm -f tests_gen/a.out

tests: tests_semantic tests_opt tests_gen

tests_semantic: tests_prepare
	# "runtest" comes from DejaGNU
	mkdir -p tests_semantic/results
	(cd tests_semantic; LD_LIBRARY_PATH=../helper/lib ../helper/bin/runtest --all --output results --tool $(BINARY) tests_semantic.exp)

tests_opt: tests_prepare
	# "runtest" comes from DejaGNU
	mkdir -p tests_opt/results
	(cd tests_opt; LD_LIBRARY_PATH=../helper/lib ../helper/bin/runtest --all --output results --tool $(BINARY) tests_opt.exp)

tests_gen: tests_prepare
	# "runtest" comes from DejaGNU
	mkdir -p tests_opt/results
	(cd tests_gen; LD_LIBRARY_PATH=../helper/lib ../helper/bin/runtest --all --output results --tool $(BINARY) tests_gen.exp)

tests_prepare:
	@echo "BINARY=../$(BINARY)" > tests_lib_shared_auto.inc

.PHONY: all lex yacc clean tests
